import * as IConstant from '../../../core/utils/IConstant';
import HttpClient from '../../../core/utils/HttpClient';
import SysUtil from '../../../core/utils/SysUtil';

export function setSelectedTabIndexAction(index)
{
    return {
        type: IConstant.Actions.SCENIC.LIST.SET_SELECTED_TAB_INDEX,
        index: index
    };
}

export function asyncGetListAction(orderFiled, orderType, pageIndex, searchKey, extraParams)
{
    return function(dispatch, getState)
    {
        dispatch(_getListRequestAction(orderFiled, orderType, pageIndex, searchKey, extraParams));
        return _fetchList(orderFiled, orderType, pageIndex, searchKey, extraParams).then((resp)=>{
            dispatch(_getListSuccessAction(orderFiled, orderType, pageIndex, searchKey, extraParams, resp));
        }).catch((error)=>{
            dispatch(_getListFailureAction(orderFiled, orderType, extraParams, error));
        });
    };
}

export function goBackAction()
{
    return {
        type: IConstant.Actions.SCENIC.LIST.GO_BACK,
    };
}

export function asyncDeleteListAction(orderFiled, orderType, extraParams)
{
    return function(dispatch, getState)
    {
        dispatch(deleteListAction(orderFiled, orderType, extraParams));
        return new Promise((resolve)=>{
            resolve();
        });
    };
}

export function deleteListAction(orderFiled, orderType, extraParams)
{
    return {
        type: IConstant.Actions.SCENIC.LIST.DELETE,
        orderFiled, 
        orderType,
        extraParams,
    };
}

export function setKeywordAction(keyword)
{
    return {
        type: IConstant.Actions.SCENIC.LIST.SET_KEYWORD,
        keyword: keyword
    };
}

export function setScrollerPositionAction(orderFiled, orderType, offset, extraParams)
{
    return {
        type: IConstant.Actions.SCENIC.LIST.SET_LISTVIEW_OFFSET,
        orderFiled, 
        orderType,
        listViewOffset: offset,
        extraParams
    };
}

function _fetchList(orderFiled, orderType, pageIndex, searchKey, extraParams)
{
    let conditionParams = {
        Order: {
            OrderField: orderFiled,
            OrderType: orderType,
        },
        Page: {
            PageIndex: pageIndex,
            PageSize: IConstant.PAGE_SIZE,
        },
    };
    if (searchKey && searchKey.length > 0)
    {
        let search = [{
            SearchField: 'Keyword',
            ConditionType: '2',
            ListSearchValue:[{
                ItemValue: searchKey
            }]
        }];
        conditionParams.Search = search;
    }
    let location = SysUtil.getLocation();
    conditionParams.Params = {
        LatItude: location.lat,
        LongItude: location.lng,  
    };
    if(extraParams && extraParams.lat && extraParams.lng)
    {
        conditionParams.Params.lat = extraParams.lat;
        conditionParams.Params.lng = extraParams.lng;
    }
    
    let kvDict = {
        ConditionParams: conditionParams,
    };
    return HttpClient.postToPortalWithUrl(HttpClient.HOST_E_BUSINESS,IConstant.PortalApis.ELEMENTS.AttractionsList, kvDict)
        .then((resp) => resp.json());
}

export function _fetchAllList(searchKey,lat,lng)
{
    let conditionParams = {
        Order: {
            OrderField: 'Sort',
            OrderType: 'desc',
        },
        Page: {
            PageIndex: 0,
            PageSize: IConstant.ALL_PAGE_SIZE,
        },
    };
    if (searchKey && searchKey.length > 0)
    {
        let search = [{
            SearchField: 'Keyword',
            ConditionType: '2',
            ListSearchValue:[{
                ItemValue: searchKey
            }]
        }];
        conditionParams.Search = search;
    }
    conditionParams.Params = {
        LatItude: lat,
        LongItude: lat,  
    };
    
    let kvDict = {
        ConditionParams: conditionParams,
    };
    return HttpClient.postToPortalWithUrl(HttpClient.HOST_E_BUSINESS,IConstant.PortalApis.ELEMENTS.AttractionsList, kvDict)
        .then((resp) => resp.json());
}

function _getListRequestAction(orderFiled, orderType, pageIndex, searchKey, extraParams)
{
    return {
        type: IConstant.Actions.SCENIC.LIST.GET_LIST_REQUEST,
        orderFiled, 
        orderType,
        pageIndex,
        extraParams,
        keyword: searchKey,
    };
}

function _getListSuccessAction(orderFiled, orderType, pageIndex, searchKey, extraParams, result)
{
    return {
        type: IConstant.Actions.SCENIC.LIST.GET_LIST_SUCCESS,
        orderFiled, 
        orderType,
        pageIndex,
        keyword: searchKey,
        extraParams,
        result
    };
}

function _getListFailureAction(orderFiled, orderType, extraParams, error)
{
    return {
        type: IConstant.Actions.SCENIC.LIST.GET_LIST_FAILURE,
        orderFiled, 
        orderType,
        extraParams,
        message: error
    };
}